package org.junyi.login.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.junyi.login.service.UserServer;
import org.junyi.pojo.User;
import org.junyi.util.ArchivesLog;
import org.junyi.util.CryptographyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import net.sf.json.JSONObject;

/**  

* <p>Title: UserController</p>  

* <p>Description: </p>  

* @author cy

* @date 2018年2月4日  

*/  
@Controller
public class UserController
{

	@Resource
	private UserServer userServer;

	/**
	 * 
	 * @param user
	 * @param request
	 * @return
	 */
	// 登录
	@RequestMapping("/login")
	@ResponseBody
	@ArchivesLog(operationType = "系统登录:", operationName = "登陆判定")
	public void login(User user, HttpServletRequest request,HttpServletResponse response) throws Exception
	{
	/*	Map<String, Object> result = new HashMap<String, Object>();*/
		Subject subject = SecurityUtils.getSubject();
		user.setPassword(CryptographyUtil.md5(user.getPassword(), "111"));
		UsernamePasswordToken token = new UsernamePasswordToken(
				user.getUserName(), user.getPassword());
		try
		{
			subject.login(token);
			HttpSession session=request.getSession();
			session.setAttribute("uname", user.getUserName());
			session.setAttribute("user", user);
			response.getWriter().print("success");
		} catch (UnknownAccountException e)
		{
			response.getWriter().print("1");
		} catch (DisabledAccountException e)
		{
			response.getWriter().print("2");
		} catch (IncorrectCredentialsException e)
		{
			response.getWriter().print("3");
		} catch (RuntimeException e)
		{
			e.printStackTrace();
			response.getWriter().print("4");
		}

	}

	// 显示出来 所有的 用户
	@RequestMapping("queryAll")
	@ResponseBody
	@ArchivesLog(operationType = "系统显示:", operationName = "显示所有用户")
	public void queryUser(String limit, String offset,
			HttpServletResponse response) throws IOException
	{
		List<User> data = userServer.query();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", data.size());//
		map.put("rows", data);//
		JSONObject jsonArray = JSONObject.fromObject(map);
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(jsonArray.toString());
	}

	// 注册的用户
	@RequestMapping("register")
	@ResponseBody
	@ArchivesLog(operationType = "系统 注册:", operationName = "注册判定")
	public void register(User user, HttpServletResponse response)
			throws IOException
	{
		String passWord = user.getPassword();
		System.out.println(CryptographyUtil.md5(passWord, "111"));
		user.setPassword(CryptographyUtil.md5(passWord, "111"));
		Integer i = userServer.add(user);
		if (i > 0)
		{
			response.setCharacterEncoding("utf-8");
			response.getWriter().write("success");
		} else
		{
			response.getWriter().write("error");
		}
	}

	// 删除 用户(只有超级管理员可以删除)
	/**
	 * @param ids
	 * @param respon
	 * @throws IOException 
	 */
	@RequestMapping("/del")
	@ResponseBody
	@ArchivesLog(operationType = "删除用户:", operationName = "超级管理员删除用户")
	public void deleteById(String ids, HttpServletResponse response) throws IOException
	{
		Integer i = userServer.DeleteByids(ids);
		if(i>0){
			response.setCharacterEncoding("utf-8");
			response.getWriter().write("success");
		} else
		{
			response.getWriter().write("error");
		}
	}
}
